package com.car.controller.custormer;

import com.car.entity.Custormer;
import com.car.service.custormer.CustormerCreditReportService;
import com.car.util.PageUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 贾勇
 * @ClassName CustormerCreditReportController
 * @Description:
 * @DateTime: 2021-12-09 10:19
 */
@RestController
public class CustormerCreditReportController {

    @Setter
    @Getter
    @Autowired
    private CustormerCreditReportService custormerCreditReportService;


    @RequestMapping("/toCredit")
    public ModelAndView toCredit() {
        return new ModelAndView("credit-list");
    }

    /**
     * 获取所有信息以及分页功能
     *
     * @param pageNum
     * @param pageSize
     * @param custormer
     * @return
     */
    @RequestMapping("/getAllCustomerCredit")
    public Map<String, Object> getAllCustomer(int pageNum, int pageSize, Custormer custormer) {

        Map<String, Object> map = new HashMap<>();

        map.put("custormerName",custormer.getCustormerName());
        map.put("custormerPhone",custormer.getCustormerPhone());
        map.put("custormer", custormer);

        Integer count = custormerCreditReportService.getAllCustomerCount(map);

        Integer total = PageUtil.getTotalPage(count, pageSize);

        map.put("total", total);

        //通过页码和条数算出本次分页的起始位置
        Integer selectstart = pageSize * (pageNum - 1);

        //通过页码和条数算出本次分页的结束位置
        Integer selectEnd = pageNum * pageSize;

        //将起始位置封装到map中，方便后续dao层调用
        map.put("selectstart", selectstart);

        //将结束位置封装到map中，方便后续dao层调用
        map.put("selectEnd", selectEnd);

        List<Custormer> custerList = custormerCreditReportService.getAllCustomer(map);

        map.put("custerList", custerList);

        return map;
    }

    /**
     * 跳转到修改页面
     * @param custormerId
     * @return
     */
    @RequestMapping("/customerCreditgetById")
    public ModelAndView customerCreditgetById(int custormerId) {

        ModelAndView mv = new ModelAndView();

        Custormer customerById = custormerCreditReportService.getCustomerById(custormerId);
        mv.addObject("customerById", customerById);
        mv.setViewName("credit-edit");
        return mv;
    }

    /**
     * 修改请求
     * @param custormer
     * @return
     */
    @RequestMapping("/updateCustomerCredit")
    public String updateCustomerCredit(Custormer custormer){
        Integer integer = custormerCreditReportService.updateCustomerCredit(custormer);
        return integer.toString();
    }
}
